/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopWallet;
import com.yami.shop.bean.model.SmsRechargeLog;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.dao.SmsRechargeLogMapper;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopWalletService;
import com.yami.shop.service.SmsPackageService;
import com.yami.shop.service.SmsRechargeLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;

/**
 *
 *
 * @author lhd
 * @date 2020-07-01 15:44:27
 */
@Service
@AllArgsConstructor
@Slf4j
public class SmsRechargeLogServiceImpl extends ServiceImpl<SmsRechargeLogMapper, SmsRechargeLog> implements SmsRechargeLogService {

    private final SmsRechargeLogMapper smsRechargeLogMapper;
    private final ShopWalletService shopWalletService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySuccess(String payNo) {
        //根据内部订单号更新商家短信购买信息
        SmsRechargeLog smsRechargeLog = getOne(new LambdaQueryWrapper<SmsRechargeLog>().eq(SmsRechargeLog::getPayNo,payNo));
        if(smsRechargeLog == null){
            log.info("订单异常,");
            return;
        }

        // 订单已支付
        if (smsRechargeLog.getIsPayed() == 1) {
            log.info("订单已支付，settlement.id：{}", smsRechargeLog.getPayNo());
            return;
        }
        //修改成已支付
        smsRechargeLog.setIsPayed(1);
        smsRechargeLog.setChargeTime(new Date());
        updateById(smsRechargeLog);

        //修改店铺钱包短信余额
        ShopWallet shopWallet = shopWalletService.getOne(new LambdaQueryWrapper<ShopWallet>().eq(ShopWallet::getShopId, smsRechargeLog.getShopId()));
        shopWallet.setSmsNum(Objects.isNull(shopWallet.getSmsNum())?smsRechargeLog.getSmsNum():smsRechargeLog.getSmsNum() + shopWallet.getSmsNum());
        shopWalletService.updateById(shopWallet);

        //添加日志


    }
}
